<?php

/**
 * Description of SettingController
 * 
 * @author trongkhoi
 * @since Jun 28, 2013 - 10:00:41 AM
 */
class SettingController extends MyController
{

    public function actionNotification()
    {
        $this->checkAccess();

        $emailSettings = EmailSetting::model()->findAll();
        $smsSetting    = Sms::model()->find();
        $this->render('notification', array(
            'emailSettings' => $emailSettings,
            'smsSetting'    => $smsSetting
        ));
    }

    public function actionEditEmail($pk)
    {
        $form_id = 'form-email-' . $pk;
        $model   = EmailSetting::model()->findByPk($pk);
        if (Yii::app()->request->isAjaxRequest && @$_POST['ajax'] == $form_id)
        {
            $model->attributes = $_POST['EmailSetting'];
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        if (isset($_POST['EmailSetting']))
        {
            $model->attributes = $_POST['EmailSetting'];
            if (!$model->validate())
            {
                Yii::app()->user->setFlash('form_error', 'Please fill correct information!');
            }
            else
            {
                if ($model->save())
                {
                    Yii::app()->user->setFlash(
                            'form_success', "Edit is successfully!"
                    );

                    $this->redirect('notification');
                }
                else
                {
                    Yii::app()->user->setFlash('form_error', 'Error, please try again!');
                }
            }
        }
    }

    public function actionEditSms()
    {
        $model = Sms::model()->find();
//        print_r($_POST);
        if (isset($_POST['usr_name']))
        {
            $model->username = $_POST['usr_name'];
        }
        if (isset($_POST['passw']))
        {
            $model->password = $_POST['passw'];
        }
        if (isset($_POST['message_1']))
        {
            $model->msg_1 = $_POST['message_1'];
        }

        if (isset($_POST['message_2']))
        {
            $model->msg_2 = $_POST['message_2'];
        }

        if (isset($_POST['message_3']))
        {
            $model->msg_3 = $_POST['message_3'];
        }

        if ($model->validate())
        {
            if ($model->save())
            {
                echo 'ok';
            }
        }
    }

}

/**
 * End of SettingController
 */
